// pages/mine/mine.js
import { ajax, src, util } from "../../utils/util.js";
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userinfo:[],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    islogin:false,//判断是否登录
    isPhoneAuth:false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var uqid = wx.getStorageSync("uqid");
    this.setData({
      uqid: uqid
    });
    // 查看是否授权
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
            }
          })
        } else {
        }
      }
    })
  },


  // 分销添加
  addyaoqing: function () {
    ajax("/api/index/distribution", {
      data: {
        uqid: wx.getStorageSync('uqid'),
        yq_uqid: wx.getStorageSync('yq_uqid')
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'post'
    }, res => {
      console.log(res)
      if (res.error == 0) {
        // wx.showToast({
        //   title: '分销成功',
        //   icon: 'none',
        //   duration: 2000
        // })
      }
    })
  },


  getcallphone:function(){
    // ajax("/api/index/contact_phone", {
    //   data: {},
    //   header: {
    //     'Accept': 'application/json', // 默认值
    //   },
    //   method: 'get'
    // }, res => {
    //   console.log(res)
    //   if (res.error == 0) {
       
    //   }
    // })
    this.setData({
      kftel: '400-102-5882'
    });
  },
  callphone: function () {
    this.setData({
      clickcall: true
    })
  },
  refusecall: function () {
    this.setData({
      clickcall: false
    })
  },
  cancall: function () {
    this.setData({
      clickcall: false
    })
    wx.makePhoneCall({
      phoneNumber: this.data.kftel //仅为示例，并非真实的电话号码
    })
  },

  //获取个人信息
  getInfo: function (uqid){
    console.log(uqid)
    ajax("/api/index/user_info", {
      data: {
        uqid: uqid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res);
      if (res.error == 0) {
        
        this.setData({
          userinfo: res.data,
          uqid: uqid,
          islogin: true
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var uqid = wx.getStorageSync('uqid');
    this.setData({
      uqid:uqid
    })
    if(uqid == ''){
      this.setData({
        islogin: false
      });
    }else{
      this.getInfo(uqid);
    }

    this.getcallphone()
  },
  //立即获取智能康复计划  && 授权
  bindGetUserInfo(e) {
    var that = this;
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var code = wx.getStorageSync("code");
      ajax('/api/index/login', {
        data: {
          js_code: code,
          nickname: e.detail.userInfo.nickName,
          avatar: e.detail.userInfo.avatarUrl
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
      }, res => {
        console.log(res)
        if (res.error == 0) {
          wx.setStorageSync("uqid", res.data.uqid)

          that.setData({
            uqid: res.data.uqid,
            session_key: res.data.session_key
          });
          this.isPhone();

          if (wx.getStorageSync('yq_uqid')) {
            if (wx.getStorageSync('yq_uqid') != this.data.uqid) {
              this.addyaoqing()
            } else {
              console.log('是本人')
            }
          }

        } else if (res.error == 4003) { //重复授权
          this.isPhone();

          if (wx.getStorageSync('yq_uqid')) {
            if (wx.getStorageSync('yq_uqid') != this.data.uqid) {
              this.addyaoqing()
            } else {
              console.log('是本人')
            }
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },

  //判断是否授权手机号
  isPhone: function () {
    var that = this;
    var tel = wx.getStorageSync("tel");
    if (tel == undefined || tel == '') { //用户没有授权手机号
      that.setData({
        isPhoneAuth: true
      });
    } else { //用户已经授权手机号
      that.setData({
        isPhoneAuth: false
      });
      // this.getTelAdd();

    }
  },

  //授权手机号
  getPhoneNumber(e) {
    this.setData({
      isPhoneAuth: false
    })
    if (e.detail.encryptedData) {
      ajax("/api/index/phone_decrypt", {
        data: {
          session: this.data.session_key,
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData
        },
        header: {
          'Accept': 'application/json', // 默认值
        },
        method: 'POST'
      }, res => {
        // console.log(res);
        if (res.error == 0) {
          var tel = res.data.phoneNumber;
          wx.setStorageSync("tel", tel)
          this.getTelAdd();
        }
      })

    } else {
      this.getInfo(this.data.uqid);
      this.setData({
        islogin:true
      })
    }
  },
  //拒绝授权手机号
  refuseUserInfo: function () {
    this.getInfo(this.data.uqid);
    this.setData({
      isPhoneAuth: false,
      islogin:true
    });
  },
  //授权手机号提交
  getTelAdd: function () {
    var tel = wx.getStorageSync("tel");
    ajax("/api/index/phone_wx_sq_add", {
      data: {
        uqid: this.data.uqid,
        phone: tel,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'POST'
    }, res => {
      console.log(res);
      if (res.error == 0) {//获取进展 pid
        this.getInfo(this.data.uqid);
      }
    })
  },


  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //我要分享
  myShare:function(){
    var uqid = this.data.uqid;
    console.log(uqid)
    if (uqid == '' || uqid == undefined ){
      wx.showToast({
        title: '请先登录！',
        icon:'none',
        duration:1000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/share/share',
    })
  },
  // 我的预约
  myReserve:function(){
    var uqid = this.data.uqid;
    if (uqid == '' || uqid == undefined ) {
      wx.showToast({
        title: '请先登录！',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/myreserve/myreserve',
    })
  },
  //我的病历
  myCase:function(){
    var uqid = this.data.uqid;
    if (uqid == '' || uqid == undefined ) {
      wx.showToast({
        title: '请先登录！',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/mycase/mycase',
    })
  },

  //我的设置
  mySetting: function () {
    var uqid = this.data.uqid;
    if (uqid == '' || uqid == undefined ) {
      wx.showToast({
        title: '请先登录！',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/personalinfo/personalinfo',
    })
  },

  //关于慧复
  abouts: function () {
    var uqid = this.data.uqid;
    if (uqid == '' || uqid == undefined ) {
      wx.showToast({
        title: '请先登录！',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/abouts/abouts',
    })
  },

  
  onPullDownRefresh: function () {
    console.log("sssssssss")
    wx.stopPullDownRefresh();
  },


})